//
//  XLLoginView.m
//  industry
//
//  Created by xl on 2020/11/20.
//  Copyright © 2020 Haier. All rights reserved.
//

#import "XLLoginView.h"
#import "XLImageAndTitleButton.h"
#import "XLVerificationCodeButton.h"
#import "XLLoginRegisterInteractorProtocol.h"
#import "XLLoginRegisterPresenterProtocol.h"
#import "XLUserDefaults.h"
#import "XLLoginModel.h"
#import <YYText.h>
#import "NSString+XL.h"
#import "YQIdentityView.h"
#import "WXApi.h"
#import <AuthenticationServices/AuthenticationServices.h>
#import "YQCheckOtherLogin.h"

#define EmptyString @"              "


@interface XLLoginView ()<XLVerificationCodeButtonDelegate,WXApiDelegate,ASAuthorizationControllerDelegate,ASAuthorizationControllerPresentationContextProviding>

@property (nonatomic, strong) XLButton *backBtn;
@property (nonatomic, strong) YYLabel *titleLB;
@property (nonatomic, strong) XLImageView *circleBgIG;
@property (nonatomic, strong) XLLabel *adLB;
@property (nonatomic, strong) XLView *adLeftLineView;
@property (nonatomic, strong) XLView *adRightLineView;

@property (nonatomic, strong) XLLabel *accountLB;
@property (nonatomic, strong) XLTextField *accountTF;
@property (nonatomic, strong) XLView *accountLineView;
@property (nonatomic, strong) XLLabel *passwordLB;
@property (nonatomic, strong) XLTextField *passwordTF;
@property (nonatomic, strong) XLView *passwordLineView;
@property (nonatomic, strong) XLImageAndTitleButton *remmberBtn;
@property (nonatomic, strong) XLButton *forgetBtn;
@property (nonatomic, strong) XLButton *loginBtn;
@property (nonatomic, strong) XLButton *changeLoginBtn;
@property (nonatomic, strong) XLVerificationCodeButton *vfCodeBtn;
@property (nonatomic, strong) YQIdentityView *identityBtn;
@property (nonatomic, strong) XLLabel *checkLB;
@property (nonatomic, strong) XLTextField *checkTF;
@property (nonatomic, strong) XLImageView *checkIG;
@property (nonatomic, strong) XLView *checkLineView;
@property (nonatomic, strong) XLButton *weixButton;
@property (nonatomic, strong) XLButton *appleButton;

@property (nonatomic, assign) BOOL isDefaultLogin;

@end
@implementation XLLoginView

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor whiteBgColor];
        UIBezierPath *cornerRaduisPath = [UIBezierPath bezierPathWithRoundedRect:self.bounds byRoundingCorners:UIRectCornerTopLeft cornerRadii:CGSizeMake(50, 50)];
        CAShapeLayer *cornerRadiusLayer = [[CAShapeLayer alloc] init];
        cornerRadiusLayer.frame = self.bounds;
        cornerRadiusLayer.path = cornerRaduisPath.CGPath;
        self.layer.mask = cornerRadiusLayer;
        
        [self addSubview:self.backBtn];
        [self addSubview:self.titleLB];
        [self addSubview:self.circleBgIG];
        [self addSubview:self.adLB];
        [self addSubview:self.adLeftLineView];
        [self addSubview:self.adRightLineView];
        
        self.isDefaultLogin = YES;
        [self addSubview:self.accountLB];
        [self addSubview:self.accountTF];
        [self addSubview:self.accountLineView];
        [self addSubview:self.passwordLB];
        [self addSubview:self.passwordTF];
        [self addSubview:self.passwordLineView];
        [self addSubview:self.remmberBtn];
        [self addSubview:self.forgetBtn];
        [self addSubview:self.loginBtn];
        [self addSubview:self.changeLoginBtn];
        [self addSubview:self.vfCodeBtn];
        [self addSubview:self.identityBtn];
        [self addSubview:self.checkLB];
        [self addSubview:self.checkTF];
        [self addSubview:self.checkIG];
        [self addSubview:self.checkLineView];
        [self addSubview:self.weixButton];
        [self addSubview:self.appleButton];
        [self makeConstraints];
    }
    return self;
}

#pragma mark - Pulic
- (void)refreshVerificationCodeButtonState:(BOOL)state {
    if (state) {//获取校验码成功
        [_vfCodeBtn starCountTime];
    } else {//获取校验码失败
        [_vfCodeBtn lockVerificationCodeEnable:YES];
    }
}

- (void)refreshCheckImage:(NSString *)url {
    [_checkIG sd_setImageWithURL:XLWebIMAGEURL(url)];
}

- (void)refreshStaffLoginSelectBtnStatus:(BOOL)status
                                  isShow:(BOOL)isShow {
    _identityBtn.hidden = !isShow;
    _identityBtn.selected = status;
    if (_identityBtn.hidden) {
        [_weixButton mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.centerY.equalTo(self.identityBtn);
            make.centerX.equalTo(self).multipliedBy(0.7);
        }];
        [_weixButton setTitle:EmptyString forState:UIControlStateNormal];
        
        [_appleButton mas_updateConstraints:^(MASConstraintMaker *make) {
            make.centerX.equalTo(self).multipliedBy(1.3);
        }];
        [_appleButton setTitle:EmptyString forState:UIControlStateNormal];
    }else{
        [_appleButton setTitle:@"AppleID" forState:UIControlStateNormal];
        [_weixButton setTitle:@"微信登录" forState:UIControlStateNormal];
        [_weixButton mas_remakeConstraints:^(MASConstraintMaker *make) {
    //        make.bottom.equalTo(self.adLB.mas_top).offset(-31);
            make.centerY.equalTo(self.identityBtn);
            make.centerX.equalTo(self).multipliedBy(1);
        }];
        
        [_appleButton mas_updateConstraints:^(MASConstraintMaker *make) {
            make.centerX.equalTo(self).multipliedBy(1.45);
        }];
       
        
    }
    
}

#pragma mark - UIButton 点击事件
- (void)clickRemmber:(XLButton *)sender {
    sender.selected = !sender.selected;
}

- (void)clickForget {
    XLSend(self.baseController.interactor, XLLoginRegisterInteractorProtocol, gotoForgetPSNextVC:_identityBtn.isSelected);
}

- (void)clickGotoRegister {
    XLSend(self.baseController.interactor, XLLoginRegisterInteractorProtocol, gotoRegistrNextVC:_identityBtn.isSelected);
}

- (void)clickLogin {
    [self endEditing:YES];
    
    if (_isDefaultLogin) {//账户登录
        if (ISEmptyString(_accountTF.text)) {
            [XLToast showErrorMessage:@"请输入您的用户名或手机号"];
            return;
        }
        if (ISEmptyString(_passwordTF.text)) {
            [XLToast showErrorMessage:@"请输入您的密码"];
            return;
        }
        XLLoginModel *model =[[XLLoginModel alloc] init];
        model.account = _accountTF.text;
        model.password = _passwordTF.text;
        model.isStafff = _identityBtn.isSelected;
        model.isRecordPS = _remmberBtn.isSelected;
        XLSend(self.baseController.presenter, XLLoginRegisterPresenterProtocol, requestFetchAESKey:XLFetchAESTypeLogin model:model);
        
    } else {//校验码登录
        if (ISEmptyString(_accountTF.text)) {
            [XLToast showErrorMessage:@"请输入您的手机号"];
            return;
        }
        if (ISEmptyString(_passwordTF.text)) {
            [XLToast showErrorMessage:@"请输入您的短信验证码"];
            return;
        }
        if (ISEmptyString(_checkTF.text)) {
            [XLToast showErrorMessage:@"请输入您的验证码"];
            return;
        }
        XLSend(self.baseController.presenter, XLLoginRegisterPresenterProtocol, requestSMSCodeLoginPhone:_accountTF.text verifyCode:_passwordTF.text isStaff:_identityBtn.isSelected);
    }
}

- (void)clickChangeLoginWay:(XLButton *)sender {
    if ([sender.titleLabel.text isEqualToString:@"使用验证码登录"]) {
        _isDefaultLogin = NO;
        [sender setTitle:@"使用密码登录" forState:UIControlStateNormal];
        [self changeLoginWayState:NO];
        
        //获取最新校验码
        [self refreshCheckImage];
    } else {
        _isDefaultLogin = YES;
        [sender setTitle:@"使用验证码登录" forState:UIControlStateNormal];
        [self changeLoginWayState:YES];
    }
}

- (void)clickIdentityChange:(XLButton *)sender {
    sender.selected = !sender.selected;
}

- (void)clickFetchImageVerify {
    [self refreshCheckImage];
}

#pragma mark - XLVerificationCodeButton
- (void)willGetVerificationCode {
    if (ISEmptyString(_accountTF.text)) {
        [XLToast showComonMessage:@"手机号不能为空"];
        return;
    }
    if (ISEmptyString(_checkTF.text)) {
        [XLToast showComonMessage:@"请先填写校验码"];
        return;
    }
    //获取登录校验码
    XLSend(self.baseController.presenter, XLLoginRegisterPresenterProtocol, requestCheckImageCode:_checkTF.text phone:_accountTF.text numberType:3);
}

#pragma mark - Private
- (void)makeConstraints {
    [_backBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self).offset(30);
        make.top.equalTo(self).offset(30);
        make.size.mas_equalTo(CGSizeMake(24, 24));
    }];
    
    [_titleLB mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self).offset(-20);
        make.centerY.equalTo(self.backBtn);
    }];
    
    [_circleBgIG mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.right.equalTo(self);
        make.size.mas_equalTo(CGSizeMake(216, 188));
    }];
    
    [_adLB mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self);
        make.bottom.equalTo(self).offset(-SAFEAREA_BOTTOM_HEIGHT - 15);
    }];
    [_adLB sizeToFit];
    
    [_adLeftLineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.adLB);
        make.right.equalTo(self.adLB.mas_left).offset(-10);
        make.left.equalTo(self).offset(26);
        make.height.mas_equalTo(1);
    }];
    
    [_adRightLineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.adLB);
        make.left.equalTo(self.adLB.mas_right).offset(10);
        make.right.equalTo(self).offset(-26);
        make.height.mas_equalTo(1);
    }];
    
    [_accountLB mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self).offset(30);
        make.top.equalTo(self).offset(70);
    }];
    [_accountLB sizeToFit];
    
    [_accountTF mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.accountLB);
        make.top.equalTo(self.accountLB.mas_bottom).offset(3);
        make.right.equalTo(self).offset(-30);
        make.height.mas_equalTo(40);
    }];
    
    [_accountLineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(self.accountTF);
        make.top.equalTo(self.accountTF.mas_bottom);
        make.height.mas_equalTo(1);
    }];
    
    [_passwordLB mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.accountLB);
        make.top.equalTo(self.accountLineView.mas_bottom).offset(5);
    }];
    
    [_passwordTF mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.passwordLB);
        make.top.equalTo(self.passwordLB.mas_bottom).offset(3);
        make.right.equalTo(self).offset(-30);
        make.height.mas_equalTo(40);
    }];
    
    [_passwordLineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self).offset(30);
        make.right.equalTo(self).offset(-30);
        make.top.equalTo(self.passwordTF.mas_bottom);
        make.height.mas_equalTo(1);
    }];
    
    [_remmberBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.passwordLineView);
        make.top.equalTo(self.passwordLineView.mas_bottom).offset(10);
        make.size.mas_equalTo(CGSizeMake(80, 30));
    }];
    
    [_forgetBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.passwordLineView);
        make.top.equalTo(self.remmberBtn);
        make.size.mas_equalTo(CGSizeMake(70, 30));
    }];
    
    [_loginBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self).offset(30);
        make.right.equalTo(self).offset(-30);
        make.top.equalTo(self.passwordLineView.mas_bottom).offset(50);
        make.height.mas_equalTo(45);
    }];
    
    [_changeLoginBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.loginBtn);
        make.top.equalTo(self.loginBtn.mas_bottom).offset(15);
    }];
    [_changeLoginBtn sizeToFit];
    
    [_vfCodeBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self).offset(-30);
        make.centerY.equalTo(self.passwordTF);
        make.size.mas_equalTo(CGSizeMake(90, 32));
    }];
    
    [_identityBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(self.adLB.mas_top).offset(-17);
        make.centerX.equalTo(self).multipliedBy(0.5);
        make.size.mas_offset(CGSizeMake(77, 67));
    }];
    [_weixButton mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.bottom.equalTo(self.adLB.mas_top).offset(-31);
        make.centerY.equalTo(self.identityBtn);
        make.centerX.equalTo(self).multipliedBy(1);
    }];
    [_appleButton mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.bottom.equalTo(self.adLB.mas_top).offset(-31);
        make.centerY.equalTo(self.identityBtn);
        make.centerX.equalTo(self).multipliedBy(1.5);
    }];
    
    [_checkLB mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self).offset(30);
        make.top.equalTo(self.passwordLineView.mas_bottom).offset(5);
    }];
    [_checkLB sizeToFit];
    
    [_checkTF mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.checkLB);
        make.top.equalTo(self.checkLB.mas_bottom);
        make.height.mas_equalTo(40);
        make.right.equalTo(self).offset(-95 - 30);
    }];
    
    [_checkIG mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self).offset(-30);
        make.centerY.equalTo(self.checkTF);
        make.size.mas_equalTo(CGSizeMake(90, 30));
    }];
    
    [_checkLineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self).offset(30);
        make.right.equalTo(self).offset(-30);
        make.top.equalTo(self.checkTF.mas_bottom);
        make.height.mas_equalTo(1);
    }];
}

- (void)changeLoginWayState:(BOOL)state {
    [self endEditing:YES];
    _accountTF.text = @"";
    _passwordTF.text = @"";
    _checkTF.text = @"";
    if (state) {//默认账号登录界面
        _vfCodeBtn.hidden = YES;
        _checkLB.hidden = YES;
        _checkTF.hidden = YES;
        _checkIG.hidden = YES;
        _checkLineView.hidden = YES;
        
        _remmberBtn.hidden = NO;
        _forgetBtn.hidden = NO;
        _accountTF.placeholder = @"请输入您的用户名/手机号";
        _passwordTF.placeholder = @"请输入密码";
        _passwordLB.text = @"密码";
        _passwordTF.secureTextEntry = YES;
        [_passwordTF mas_updateConstraints:^(MASConstraintMaker *make) {
            make.right.equalTo(self).offset(-30);
        }];
        [_loginBtn mas_updateConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.passwordLineView.mas_bottom).offset(50);
        }];
        
        NSString *account = [XLUserDefaults getLastLoginPhone];
        if (!ISEmptyString(account)) {
            _accountTF.text = account;
        }
        
    } else {
        _vfCodeBtn.hidden = NO;
        _checkLB.hidden = NO;
        _checkTF.hidden = NO;
        _checkIG.hidden = NO;
        _checkLineView.hidden = NO;
        _remmberBtn.hidden = YES;
        _forgetBtn.hidden = YES;
        _accountTF.placeholder = @"请输入您的手机号";
        _passwordTF.placeholder = @"请输入短信验证码";
        _passwordLB.text = @"短信验证码";
        _passwordTF.secureTextEntry = NO;
        [_passwordTF mas_updateConstraints:^(MASConstraintMaker *make) {
            make.right.equalTo(self).offset(-30 - 95);
        }];
        [_loginBtn mas_updateConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.passwordLineView.mas_bottom).offset(80);
        }];
    }
}

- (void)refreshCheckImage {
    NSString *url = [NSString stringWithFormat:@"%@%@", XLServerHttpDomain, XLFetchImageVerifyUrl];
    UIImage *image = [UIImage imageWithData:[NSData dataWithContentsOfURL:XLWebIMAGEURL(url)]];
    _checkIG.image = image;
}

#pragma mark - setter/getter
- (XLButton *)backBtn {
    if (!_backBtn) {
        _backBtn = [XLButton new];
        [_backBtn setImage:XLIMAGE(@"back") forState:UIControlStateNormal];
    }
    return _backBtn;
}

- (YYLabel *)titleLB {
    if (!_titleLB) {
        _titleLB = [YYLabel new];
        NSString *title = @"还没有账号？立即注册";
        NSMutableAttributedString *attri = [[NSMutableAttributedString alloc] initWithString:title];
        [attri yy_setAttributes:@{NSFontAttributeName:[UIFont fontOfPingFangSCSemiboldWithSize:14], NSForegroundColorAttributeName:[UIColor blackFontColor]}];
        [attri yy_setTextHighlightRange:[title rangeOfString:@"立即注册"] color:[UIColor blueFontColor] backgroundColor:[UIColor clearColor] tapAction:^(UIView * _Nonnull containerView, NSAttributedString * _Nonnull text, NSRange range, CGRect rect) {
                XLLog(@"点击了协议");
            [self clickGotoRegister];
        }];
        _titleLB.attributedText = attri;
        _titleLB.textAlignment = NSTextAlignmentCenter;
        
        CGFloat width = [title calculateRowYYLabelWidthWithAttri:attri height:20];
        _titleLB.preferredMaxLayoutWidth = width;
    }
    return _titleLB;
}

- (XLLabel *)adLB {
    if (!_adLB) {
        _adLB = [XLLabel new];
        _adLB.font = [UIFont fontOfPingFangSCRegularWithSize:11];
        _adLB.textColor = [UIColor grayFontColor];
        _adLB.text = @"为产业而金融，因科技而不同";
    }
    return _adLB;
}

- (XLView *)adLeftLineView {
    if (!_adLeftLineView) {
        _adLeftLineView = [XLView new];
        _adLeftLineView.backgroundColor = [UIColor grayLineColor];
    }
    return _adLeftLineView;
}

- (XLView *)adRightLineView {
    if (!_adRightLineView) {
        _adRightLineView = [XLView new];
        _adRightLineView.backgroundColor = [UIColor grayLineColor];
    }
    return _adRightLineView;
}

- (XLLabel *)accountLB {
    if (!_accountLB) {
        _accountLB = [XLLabel new];
        _accountLB.font = [UIFont fontOfPingFangSCSemiboldWithSize:15];
        _accountLB.textColor = [UIColor blackFontColor];
        _accountLB.text = @"用户名/手机号";
    }
    return _accountLB;
}

- (XLTextField *)accountTF {
    if (!_accountTF) {
        _accountTF = [XLTextField new];
        _accountTF.font = [UIFont fontOfPingFangSCRegularWithSize:16];
        _accountTF.placeholder = @"请输入您的用户名/手机号";
        _accountTF.tintColor = [UIColor redFontColor];
        _accountTF.textColor = [UIColor blackFontColor];
        _accountTF.enablesReturnKeyAutomatically = YES;
        _accountTF.returnKeyType = UIReturnKeyNext;
        NSString *account = [XLUserDefaults getLastLoginPhone];
        if (!ISEmptyString(account)) {
            _accountTF.text = account;
        }
    }
    return _accountTF;
}

- (XLView *)accountLineView {
    if (!_accountLineView) {
        _accountLineView = [XLView new];
        _accountLineView.backgroundColor = [UIColor grayLineColor];
    }
    return _accountLineView;
}

- (XLLabel *)passwordLB {
    if (!_passwordLB) {
        _passwordLB = [XLLabel new];
        _passwordLB.font = [UIFont fontOfPingFangSCSemiboldWithSize:15];
        _passwordLB.textColor = [UIColor blackFontColor];
        _passwordLB.text = @"密码";
    }
    return _passwordLB;
}

- (XLTextField *)passwordTF {
    if (!_passwordTF) {
        _passwordTF = [XLTextField new];
        _passwordTF.font = [UIFont fontOfPingFangSCRegularWithSize:16];
        _passwordTF.placeholder = @"请输入您的密码";
        _passwordTF.tintColor = [UIColor redFontColor];
        _passwordTF.textColor = [UIColor blackFontColor];
        _passwordTF.enablesReturnKeyAutomatically = YES;
        _passwordTF.secureTextEntry = YES;
        _passwordTF.returnKeyType = UIReturnKeyNext;
        
        NSString *password = [XLUserDefaults getLastLoginPassword];
        if (!ISEmptyString(password)) {
            _passwordTF.text = password;
        }
    }
    return _passwordTF;
}

- (XLView *)passwordLineView {
    if (!_passwordLineView) {
        _passwordLineView = [XLView new];
        _passwordLineView.backgroundColor = [UIColor grayLineColor];
    }
    return _passwordLineView;
}

- (XLImageAndTitleButton *)remmberBtn {
    if (!_remmberBtn) {
        _remmberBtn = [[XLImageAndTitleButton alloc] initWithFrame:CGRectZero imageSize:CGSizeMake(15, 15) imageAndTitleSpace:5 imageRelativeTitlePosition:XLImageRelativeTitlePositionLeft];
        _remmberBtn.titleLabel.font = [UIFont fontOfPingFangSCRegularWithSize:13];
        [_remmberBtn setImage:XLIMAGE(@"check_n") forState:UIControlStateNormal];
        [_remmberBtn setImage:XLIMAGE(@"check_s") forState:UIControlStateSelected];
        [_remmberBtn setTitleColor:[UIColor grayFontColor] forState:UIControlStateNormal];
        [_remmberBtn setTitle:@"记住密码" forState:UIControlStateNormal];
        [_remmberBtn addTarget:self action:@selector(clickRemmber:) forControlEvents:UIControlEventTouchUpInside];
        _remmberBtn.selected = YES;
    }
    return _remmberBtn;
}

- (XLButton *)forgetBtn {
    if (!_forgetBtn) {
        _forgetBtn = [XLButton new];
        _forgetBtn.titleLabel.font = [UIFont fontOfPingFangSCRegularWithSize:13];
        [_forgetBtn setTitleColor:[UIColor grayFontColor] forState:UIControlStateNormal];
        [_forgetBtn setTitle:@"忘记密码？" forState:UIControlStateNormal];
        [_forgetBtn addTarget:self action:@selector(clickForget) forControlEvents:UIControlEventTouchUpInside];
    }
    return _forgetBtn;
}

- (XLButton *)loginBtn {
    if (!_loginBtn) {
        _loginBtn = [XLButton new];
        _loginBtn.layer.cornerRadius = 22.5f;
        _loginBtn.layer.masksToBounds = YES;
        _loginBtn.backgroundColor = [UIColor blueBgColor];
        _loginBtn.titleLabel.font = [UIFont fontOfPingFangSCSemiboldWithSize:16];
        [_loginBtn setTitleColor:[UIColor whiteFontColor] forState:UIControlStateNormal];
        [_loginBtn setTitle:@"登录" forState:UIControlStateNormal];
        [_loginBtn addTarget:self action:@selector(clickLogin) forControlEvents:UIControlEventTouchUpInside];
    }
    return _loginBtn;
}

- (XLButton *)changeLoginBtn {
    if (!_changeLoginBtn) {
        _changeLoginBtn = [XLButton new];
        _changeLoginBtn.titleLabel.font = [UIFont fontOfPingFangSCRegularWithSize:14];
        [_changeLoginBtn setTitleColor:[UIColor grayFontColor] forState:UIControlStateNormal];
        [_changeLoginBtn setTitle:@"使用验证码登录" forState:UIControlStateNormal];
        [_changeLoginBtn addTarget:self action:@selector(clickChangeLoginWay:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _changeLoginBtn;
}

- (XLVerificationCodeButton *)vfCodeBtn {
    if (!_vfCodeBtn) {
        _vfCodeBtn = [XLVerificationCodeButton new];
        _vfCodeBtn.buttonDelegate = self;
        _vfCodeBtn.hidden = YES;
    }
    return _vfCodeBtn;
}

- (YQIdentityView *)identityBtn {
    if (!_identityBtn) {
        _identityBtn = [[YQIdentityView alloc] init];
        
        _identityBtn.hidden = YES;
    }
    return _identityBtn;
}

- (XLButton *)weixButton
{
    if(!_weixButton) {
        _weixButton = ({
            XLButton * weixButton = [[XLButton alloc] init];
            [weixButton setImage:XLIMAGE(@"weixinLogin") forState:UIControlStateNormal];
            [weixButton setTitle:EmptyString forState:UIControlStateNormal];
            [weixButton setBounds:CGRectMake(0, 0, 67, 67)];
            [weixButton.titleLabel setFont:[UIFont systemFontOfSize:14]];
            [weixButton verticalImageAndTitle:7];
            [weixButton setTitleColor:[UIColor colorWithHex:0x353535] forState:UIControlStateNormal];
            WEAKSELF
            [weixButton setButtonAction:^(XLButton * _Nonnull button) {
                [weakSelf sendAuthRequest];
            }];
            weixButton;
        });
    }
    return _weixButton;
}

- (XLButton *)appleButton
{
    if(!_appleButton) {
        _appleButton = ({
            XLButton * appleButton = [[XLButton alloc] init];
//            UIImage * image = [UIImage systemImageNamed:@"applelogo" compatibleWithTraitCollection:[UITraitCollection traitCollectionWithUserInterfaceStyle:UIUserInterfaceStyleDark]];
//            let smallConfiguration = UIImage.SymbolConfiguration(scale: .small)
//            let smallSymbolImage = UIImage(systemName: "square.and.pencil", withConfiguration: smallConfiguration)
//            UIImage * image;
//            if (@available(iOS 13.0, *)) {
//
//                UIImageSymbolConfiguration * configuration = [UIImageSymbolConfiguration configurationWithPointSize:40 weight:UIImageSymbolWeightThin scale:UIImageSymbolScaleSmall];
//
//
//                image = [UIImage systemImageNamed:@"applelogo" withConfiguration:configuration];
//
//                [appleButton setImage:image forState:UIControlStateNormal];
//            } else {
//                // Fallback on earlier versions
//            }
//
//            appleButton.tintColor = [UIColor whiteColor];
//            [appleButton.imageView setBackgroundColor: [UIColor blackColor]];
//            [appleButton setBounds:CGRectMake(0, 0, 40, 40)];
//            UIImageSymbolConfiguration * configuration = [UIImageSymbolConfiguration configurationWithPointSize:40 weight:UIImageSymbolWeightThin scale:UIImageSymbolScaleSmall];
//            UIImage * image = [UIImage imageNamed:@"White Logo Square"inBundle:[NSBundle mainBundle] withConfiguration:configuration];
            
//            image.size = CGSizeMake(40, 40);
            
            [appleButton setImage:XLIMAGE(@"appleid") forState:UIControlStateNormal];
            [appleButton setTitle:EmptyString forState:UIControlStateNormal];
            [appleButton setBounds:CGRectMake(0, 0, 67, 67)];
            [appleButton.titleLabel setFont:[UIFont systemFontOfSize:14]];
            [appleButton verticalImageAndTitle:7];
            [appleButton setTitleColor:[UIColor colorWithHex:0x353535] forState:UIControlStateNormal];
            WEAKSELF
            [appleButton setButtonAction:^(XLButton * _Nonnull button) {
                if (@available(iOS 13.0, *)) {
                    [weakSelf sendAppleAuthRequest];
                } else {
                    [XLToast showErrorMessage:@"当前系统版本不支持apple登录"];
                }

            }];
            appleButton;
        });
    }
    return _appleButton;
}

-(void)sendAuthRequest
{
    //构造SendAuthReq结构体
    SendAuthReq* req =[[SendAuthReq alloc]init];
    req.scope = @"snsapi_userinfo";
    req.state = [NSString stringWithFormat:@"%d",self.identityBtn.selected];
    //第三方向微信终端发送一个SendAuthReq消息结构
    
    [WXApi sendAuthReq:req viewController:self.baseController delegate:self completion:^(BOOL success) {
            NSLog(@"进入微信请求授权页面%d",success);
    }];
    
}

-(void)sendAppleAuthRequest
{
    if (@available(iOS 13.0, *)) {
    ASAuthorizationAppleIDProvider * appleIDProvider = [[ASAuthorizationAppleIDProvider alloc]init];

    ASAuthorizationAppleIDRequest * request = [appleIDProvider createRequest];
    
//    [request setRequestedScopes:@[ASAuthorizationScopeFullName,ASAuthorizationScopeEmail]];
    
    
        ASAuthorizationController * authorizationController = [[ASAuthorizationController alloc]initWithAuthorizationRequests:@[request]];
        authorizationController.delegate = self;

        authorizationController.presentationContextProvider = self;
        [authorizationController performRequests];
    }
    
}

- (void)authorizationController:(ASAuthorizationController *)controller didCompleteWithError:(NSError *)error API_AVAILABLE(ios(13.0)){
    [XLToast showErrorMessage:[NSString stringWithFormat:@"苹果授权失败，请稍后再试！"]];
    NSLog(@"error %@",error);
}

- (void)authorizationController:(ASAuthorizationController *)controller didCompleteWithAuthorization:(ASAuthorization *)authorization API_AVAILABLE(ios(13.0)){
    if (@available(iOS 13.0, *)) {
    ASAuthorizationAppleIDCredential * credential = (ASAuthorizationAppleIDCredential *)authorization.credential;
    NSLog(@"credential %@",credential);
    [YQCheckOtherLogin loginRepuest:credential.user withType:XLLoginBindApple andStaffState:self.identityBtn.selected andCurrentVC:self.currentTopController];
    
//    user: Unique, stable, team-scoped user ID，苹果用户唯一标识符，该值在同一个开发者账号下的所有 App 下是一样的，开发者可以用该唯一标识符与自己后台系统的账号体系绑定起来（这与国内的微信、QQ、微博等第三方登录流程基本一致）。
//    identityToken: Identity token, code，验证数据，用于传给开发者后台服务器，然后开发者服务器再向苹果的身份验证服务端验证本次授权登录请求数据的有效性和真实性，详见 Sign In with Apple REST API。如果验证成功，可以根据 userIdentifier 判断账号是否已存在，若存在，则返回自己账号系统的登录态，若不存在，则创建一个新的账号，并返回对应的登录态给 App。
//    realUserStatus: High confidence indicator that likely real user，用于判断当前登录的苹果账号是否是一个真实用户，取值有：unsupported、unknown、likelyReal。
    }
}




- (ASPresentationAnchor)presentationAnchorForAuthorizationController:(ASAuthorizationController *)controller API_AVAILABLE(ios(13.0)){
    return self.window;
}

- (XLImageView *)circleBgIG {
    if (!_circleBgIG) {
        _circleBgIG = [XLImageView new];
        _circleBgIG.image = XLIMAGE(@"tybg");
    }
    return _circleBgIG;
}

- (XLLabel *)checkLB {
    if (!_checkLB) {
        _checkLB = [XLLabel new];
        _checkLB.font = [UIFont fontOfPingFangSCSemiboldWithSize:15];
        _checkLB.textColor = [UIColor blackFontColor];
        _checkLB.text = @"验证码";
        _checkLB.hidden = YES;
    }
    return _checkLB;
}

- (XLTextField *)checkTF {
    if (!_checkTF) {
        _checkTF = [XLTextField new];
        _checkTF.font = [UIFont fontOfPingFangSCRegularWithSize:16];
        _checkTF.placeholder = @"请输入验证码";
        _checkTF.tintColor = [UIColor redFontColor];
        _checkTF.textColor = [UIColor blackFontColor];
        _checkTF.enablesReturnKeyAutomatically = YES;
        _checkTF.returnKeyType = UIReturnKeyNext;
        _checkTF.hidden = YES;
    }
    return _checkTF;
}

- (XLView *)checkLineView {
    if (!_checkLineView) {
        _checkLineView = [XLView new];
        _checkLineView.backgroundColor = [UIColor grayLineColor];
        _checkLineView.hidden = YES;
    }
    return _checkLineView;
}

- (XLImageView *)checkIG {
    if (!_checkIG) {
        _checkIG = [XLImageView new];
        _checkIG.hidden = YES;
        _checkIG.userInteractionEnabled = YES;
        
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(clickFetchImageVerify)];
        [_checkIG addGestureRecognizer:tap];
        
    }
    return _checkIG;
}



@end
